package com.example.tool.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.tool.util.DebugLog;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by Carson_Ho on 17/6/8.
 */
public class DividerItemAxleDecoration extends RecyclerView.ItemDecoration {
    int n = 6;
    // 写右边字的画笔(具体信息)
    private Paint mPaint; //一般的画笔
    private Paint pointToPaint; //到点的绘制画笔

    // 写左边日期字的画笔( 时间 + 日期)
    private Paint mPaint1;
    private Paint mPaintGray;

    // 左 上偏移长度
    private int itemView_leftinterval;
    private int itemView_topinterval;

    // 轴点半径
    private int circle_radius;

    // 图标
    private Bitmap mIcon;
    private final List<HashMap<String, Object>> mapList; //数据


    // 在构造函数里进行绘制的初始化，如画笔属性设置等
    public DividerItemAxleDecoration(Context context, List<HashMap<String, Object>> hashMapList, boolean initCanvas) {
        mapList = hashMapList;
        // 轴点画笔
        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);
        //绘制到点的
        pointToPaint = new Paint();
        pointToPaint.setColor(Color.BLUE);

        // 左边时间文本画笔(蓝色)
        // 此处设置了两只分别设置 时分 & 年月
        mPaint1 = new Paint();
        mPaint1.setColor(Color.BLUE);
        mPaint1.setTextSize(30);

        mPaintGray = new Paint();
        mPaintGray.setColor(Color.GRAY);
        mPaintGray.setTextSize(30);

        // 赋值ItemView的左偏移长度为200
        itemView_leftinterval = 200;

        // 赋值ItemView的上偏移长度为50
        itemView_topinterval = 30;

        // 赋值轴点圆的半径为10
        circle_radius = 10;

    }

    // 重写getItemOffsets（）方法
    // 作用：设置ItemView 左 & 上偏移长度
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        // 设置ItemView的左 & 上偏移长度分别为200 px & 50px,即此为onDraw()可绘制的区域
        outRect.set(itemView_leftinterval, itemView_topinterval, 0, 0);

    }

    // 重写onDraw（）
    // 作用:在间隔区域里绘制时光轴线 & 时间文本
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        // 获取RecyclerView的Child view的个数
//        int childCount = parent.getChildCount();
//        DebugLog.w("DividerItemAxleDecoration--开始初始化....childCount......."+childCount);
        for (int i = 0; i < mapList.size(); i++) {
            // 获取每个Item对象
            View child = parent.getChildAt(i);
            if (child != null) {
                /**
                 * 绘制到点轴点
                 */
                // 轴点 = 圆 = 圆心(x,y)
                float centerx = child.getLeft() - itemView_leftinterval / 3f;
                float centery = child.getTop() - itemView_topinterval + (itemView_topinterval + child.getHeight()) / 2f;

                /**
                 * 绘制上半轴线
                 */
                // 上端点坐标(x,y)
                float upLine_up_x = centerx;
                float upLine_up_y = child.getTop() - itemView_topinterval;

                // 下端点坐标(x,y)
                float upLine_bottom_x = centerx;
                float upLine_bottom_y = centery + circle_radius;
                //TODO：判断是否到点
                //绘制上半部轴线
                if (i <= n) {
                    pointToPaint.setPathEffect(new DashPathEffect(new float[]{4, 4}, 0));
                    c.drawLine(upLine_up_x, upLine_up_y, upLine_bottom_x, upLine_bottom_y, pointToPaint);
                    // 绘制轴点圆
                    if (i == n) {
                        //在某个点才开始画圆
                        c.drawCircle(centerx, centery, circle_radius, pointToPaint);
                    }
                } else {
                    c.drawLine(upLine_up_x, upLine_up_y, upLine_bottom_x, upLine_bottom_y, mPaint);
//                c.drawCircle(centerx, centery, circle_radius, mPaint);
                }

                /**
                 * 绘制下半轴线
                 */
                // 上端点坐标(x,y)
                float bottomLine_up_x = centerx;
                float bottom_up_y = centery + circle_radius;
                // 下端点坐标(x,y)
                float bottomLine_bottom_x = centerx;
                float bottomLine_bottom_y = child.getBottom();
                if (i <= n - 1) {
                    //到点只画上一半
                    c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, pointToPaint);
                } else {
                    c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, mPaint);
                }
                /**
                 * 绘制左边时间文本
                 */
                // 获取每个Item的位置
                int index = parent.getChildAdapterPosition(child);
                // 设置文本起始坐标
                float Text_x = child.getLeft() - itemView_leftinterval * 5 / 6f;
                float Text_y = upLine_bottom_y;

                // 根据Item位置设置时间文本
                if (index <= n) {
                    c.drawText(Objects.requireNonNull(mapList.get(index).get("ItemTitle")).toString(), Text_x, Text_y, mPaint1);
                } else {
                    c.drawText(Objects.requireNonNull(mapList.get(index).get("ItemTitle")).toString(), Text_x, Text_y, mPaintGray);
                }

            }
        }
        // 遍历每个Item，分别获取它们的位置信息，然后再绘制对应的分割线
    }
}
